<?php
/**
 * Product class
 *
 * @package Webtaculous Shop
 * @author Colin Burn-Murdoch
 **/
class Product {
	
	/**
	 * Quantity of the product.
	 *
	 * @var int
	 * @access private
	 **/
	public $quantity;
	
	/**
	 * Database row representing the core products
	 *
	 * @var array
	 * @access private
	 **/
	private $coreproduct;
	
	/**
	 * Array of sub products beneath the core product 
	 *
	 * @var array
	 * @access private
	 **/
	private $subproduct_ids;
	
	/**
	 * The above array, in a comma seperated string
	 *
	 * @var string
	 * @access private
	 **/
	private $subproduct_ids_sql;
	
	/**
	 * Database row representing current sub product
	 *
	 * @var array
	 * @access private
	 **/
	private $subproduct;
	
	/**
	 * Currently selected attributes (based on current sub product) 
	 *
	 * @var array
	 * @access private
	 **/
	private $attributes = array();
	
	/**
	 * Currently selected simple attributes
	 *
	 * @var string
	 **/
	private $simpleAttributes = array();
	
	/**
	 * Zend_Db object
	 *
	 * @var object
	 * @access private
	 **/
	private $db;
	
	/**
	 * Cache so we don't have to do more database than we need to
	 * 
	 * @var array
	 * @access private
	 **/
	public $cache;
	
	public function __construct($id, $reconstruct_from_session=true) {
		global $db;
		$this->db = $db;
		$this->coreproduct = $db->fetchRow("SELECT * FROM shop_products WHERE id=? AND deleted=0", $id);
		if ($this->coreproduct['parent_id']) {
			$attributes = $db->fetchAll("SELECT av.attribute_id, av.value AS selected FROM shop_products_attributes_values av JOIN shop_products_attributes_products ap ON ap.attribute_id=av.attribute_id WHERE av.product_id=? AND ap.product_id=? ORDER BY ap.sort", array($id, $this->coreproduct['parent_id']));
			#echo zend_db_last_query(),"\n";
			$this->coreproduct = $db->fetchRow("SELECT * FROM shop_products WHERE id=? AND deleted=0", $this->coreproduct['parent_id']);
			
			$subproducts = $db->fetchCol("SELECT id FROM shop_products WHERE parent_id=?", $this->coreproduct['id']);
			$this->subproduct_ids = $subproducts;
			$this->subproduct_ids_sql = join(',', $subproducts);
			
		} else {
			$attributes = $db->fetchAll("SELECT attribute_id, `default` AS selected FROM shop_products_attributes_products WHERE product_id=? ORDER BY sort", $id);
			
			$subproducts = $db->fetchCol("SELECT id FROM shop_products WHERE parent_id=?", $this->coreproduct['id']);
			$this->subproduct_ids = $subproducts;
			$this->subproduct_ids_sql = join(',', $subproducts);
			
			// check session and change selected where previously selected if valid for this product
			if (isset($_SESSION['shop']['attributes']) and $reconstruct_from_session) {
				foreach($attributes as $i => $attribute) {
					if (isset($_SESSION['shop']['attributes'][$attribute['attribute_id']])) {
						if ($this->db->fetchOne("SELECT value FROM shop_products_attributes_values WHERE product_id IN ({$this->subproduct_ids_sql}) AND attribute_id=? AND value=?", array($attribute['attribute_id'], $_SESSION['shop']['attributes'][$attribute['attribute_id']]))) {
							$attributes[$i]['selected'] = $_SESSION['shop']['attributes'][$attribute['attribute_id']];
						}
					}
				}
			}
			
    	}
		#print_r($attributes);
		$this->quantity = $this->getCoalesced('minimum_quantity');
		
		
		if (count($subproducts)) {
			foreach($attributes as $attribute) {
				if ($attribute['selected']) {
					$subproducts = array_merge(array_intersect($subproducts, $db->fetchCol("SELECT * FROM shop_products_attributes_values WHERE product_id IN ({$this->subproduct_ids_sql}) AND attribute_id=? AND value=?", array($attribute['attribute_id'], $attribute['selected']))));
					$this->attributes[$attribute['attribute_id']] = $attribute['selected'];
				}
			}
			$this->subproduct = $db->fetchRow("SELECT * FROM shop_products WHERE id=? AND deleted=0", $subproducts[0]);
			$this->attributes = $db->fetchPairs("SELECT attribute_id, value FROM shop_products_attributes_values WHERE product_id=?", $this->subproduct['id']);
		}
		
		$this->simpleAttributes = $db->fetchPairs("SELECT simpleattribute_id, IF(`default`, `default`, (SELECT `value` FROM shop_products_simpleattributes_values psv WHERE (product_id=? OR product_id=?) AND psv.simpleattribute_id=psp.simpleattribute_id ORDER BY sort LIMIT 1)) FROM shop_products_simpleattributes_products psp WHERE (product_id=? OR product_id=?)", array($this->coreproduct['id'], $this->subproduct['id'], $this->coreproduct['id'], $this->subproduct['id']));
		#SELECT simpleattribute_id, IF(`default`, `default`, (SELECT `value` FROM shop_products_simpleattributes_values psv WHERE (product_id=160 OR product_id=162) AND psv.simpleattribute_id=psp.simpleattribute_id ORDER BY sort LIMIT 1)) FROM shop_products_simpleattributes_products psp WHERE (product_id=160 OR product_id=162)
		#echo zend_db_last_query();
		if ($reconstruct_from_session) {
			foreach($this->simpleAttributes as $id => $value) {
				if (isset($_SESSION['shop']['simpleattributes'][$id])) {
					if ($this->db->fetchOne("SELECT value FROM shop_products_simpleattributes_values WHERE (product_id=? OR product_id=?) AND simpleattribute_id=? AND value=?", array($this->coreproduct['id'], $this->subproduct['id'], $id, $_SESSION['shop']['simpleattributes'][$id]))
					or $this->db->fetchOne("SELECT type FROM shop_products_simpleattributes WHERE id=?", $id) != 'select') {
						$this->simpleAttributes[$id] = $_SESSION['shop']['simpleattributes'][$id];
					}
				}
			}
		}
	}
	
	/**
	 * Output an array containing the basic bits & pieces we'll need for displaying
	 * the product information.
	 *
	 * @return array
	 **/
	public function export($full=false) {
		$data =  array();
		$data['timestamp']               = $this->coreproduct['timestamp'];
		$data['code']                    = $this->coreproduct['code'];
		$data['subcode']                 = $this->subproduct['code'];
		$data['path']                    = $this->coreproduct['path'];
		$data['title']                   = $this->coreproduct['title'];
		$data['description']             = $this->coreproduct['description'];
		$data['short_description']       = $this->coreproduct['short_description'];
		$data['stock_control']           = $this->getCoalesced('stock_control');
		$data['stock_level']             = $this->subproduct ? $this->subproduct['stock_level'] : $this->coreproduct['stock_level'];
		$data['setup_price']             = $this->getCoalesced('setup_price');
		$data['minimum_quantity']        = $this->getCoalesced('minimum_quantity');
		$data['multiples_of']            = $this->getCoalesced('multiples_of');
		$data['rrp']                     = $this->getCoalesced('rrp');
		$data['weight']                  = $this->getCoalesced('weight');
		$data['vat_rate']                = $this->getCoalesced('vat_rate');
		$data['free_shipping']           = $this->getCoalesced('free_shipping');
		$data['ships_in_min']            = $this->getCoalesced('ships_in_min');
		$data['ships_in_max']            = $this->getCoalesced('ships_in_max');
		$data['quantity']                = $this->getQuantity();
		$data['price']                   = $this->getPrice();
		$data['simple_attributes_price'] = $this->getSimpleAttributesPrice();
		$data['setup_price']             = $this->getSetupPrice();
		$data['total_price_incl']        = $this->valueInclVat($this->getTotalPrice(), $this->getCoalesced('vat_rate'));
		$data['total_price_excl']        = $this->valueExclVat($this->getTotalPrice(), $this->getCoalesced('vat_rate'));
		$data['total_vat']               = $this->valueInclVat($this->getTotalPrice(), $this->getCoalesced('vat_rate')) - $this->valueExclVat($this->getTotalPrice(), $this->getCoalesced('vat_rate'));
		$data['prices']                  = $this->getPrices();
		$data['price_min']               = min(array_values($this->getPrices()));
		$data['price_max']               = max(array_values($this->getPrices()));
		$data['purchase_count']          = $this->coreproduct['purchase_count'];
		$data['images']                  = $this->getImages();
		
		if ($full) {
			$data['attributes']       = $this->attributes();
			$data['simpleattributes'] = $this->simpleAttributes();
			$data['meta']             = $this->meta();
			$data['categories']       = $this->categories();
			$data['range']            = $this->range();
			$data['related']          = $this->related();
		}
		
		return $data;
	}
	
	private function getImages() {
		if ($this->subproduct) $images = shop_get_images('products', $this->subproduct['code']);
		if (!isset($images) or !$images) $images = shop_get_images('products', $this->coreproduct['code']);
		return $images;
	}
	
	
	public function getId() {
		return $this->getCoalesced('id');
	}
	
	/**
	 * Return either the value specified by $field from the sub product, and if that's 
	 * null or array() then return from coreproduct, failing that return $default
	 *
	 * @return mixed
	 **/
	private function getCoalesced($field, $default=null) {
		return coalesce($this->subproduct[$field], $this->coreproduct[$field], $default);
	}
	
	/**
	 * Set the quantity of the product so the price can be calculated.  If the quantity
	 * requested is less that the minimum_quantity, set to minimum_quantity, otherwise
	 * if it is not a multiple of multiples_of then reduce to a value that is valid.
	 *
	 * @return int
	 **/
	public function setQuantity($quantity) {
		$minimum_quantity = $this->getCoalesced('minimum_quantity', 1);
		$multiples_of     = $this->getCoalesced('multiples_of', 1);
		
		if ($quantity >= $minimum_quantity and $quantity % $multiples_of == 0) {
			$this->quantity = $quantity;
		} elseif ($quantity < $minimum_quantity) {
			$this->quantity = $minimum_quantity;
		} else {
			$this->quantity = $quantity - ($quantity % $multiples_of);
		}
		$this->cache = array();
		return $this->quantity;
	}
	
	/**
	 * Return the current product quantity
	 *
	 * @return int
	 **/
	public function getQuantity() {
		return $this->quantity;
	}
	
	/**
	 * Get the total price, based on quantity, including the setup price
	 *
	 * @return double
	 **/
	public function getTotalPrice() {
		return $this->getSetupPrice() + $this->quantity * $this->getPrice();
	}
	
	/**
	 * Get the setup price of the product
	 *
	 * @return double
	 */
	public function getSetupPrice() {
		return $this->getCoalesced('setup_price', 0);
	}
	
	/**
	 * Get the individual product price.  This depends on the quantity
	 *
	 * @return double
	 **/
	public function getPrice() {
		if (isset($this->cache['price'])) return $this->cache['price'];
		$prices = array_reverse($this->getPrices(), true);
		foreach($prices as $minimum_quantity => $price) {
			if ($this->quantity >= $minimum_quantity) {
				$price = number_format($price, 2, '.', '');
				break;
			}
		}
		$price = $price + $this->getSimpleAttributesPrice();
		$this->cache['price'] = $price;
		return $price;
	}
	
	public function getSimpleAttributesPrice() {
		if (isset($this->cache['simpleAttributesPrice'])) return $this->cache['simpleAttributesPrice'];
		$simpleAttributesPrice = 0;
		$simpleAttributes = $this->simpleAttributes();
		if ($simpleAttributes) {
			foreach($simpleAttributes as $simpleAttribute) {
				if (isset($simpleAttribute['price'])) {
					$simpleAttributesPrice += $simpleAttribute['price'];
				}
			}
		}
		$this->cache['simpleAttributesPrice'] = $simpleAttributesPrice;
		return $simpleAttributesPrice;
	}
	
	/**
	 * Return an assoc array of $minimum_quantity => $price
	 *
	 * @return array
	 **/
	public function getPrices() {
		if (isset($this->cache['prices'])) return $this->cache['prices'];
		$core_prices = $this->db->fetchPairs("SELECT minimum_quantity, price FROM shop_products_prices WHERE product_id=? ORDER BY minimum_quantity", $this->coreproduct['id']);
		if ($this->subproduct) {
			$sub_prices = $this->db->fetchPairs("SELECT minimum_quantity, price FROM shop_products_prices WHERE product_id=? ORDER BY minimum_quantity", $this->subproduct['id']);
			$prices = coalesce($sub_prices, $core_prices, array());
		} else {
			$prices = $core_prices;
		}
		$this->cache['prices'] = $prices;
		return $prices;
	}
	
	/**
	 * Set an attribute to a value. This will force the sub product to 
	 * regenerate.
	 *
	 * @return void
	 **/
	public function setAttribute($attribute_id, $value) {
		$this->attributes[$attribute_id] = $value;
		$_SESSION['shop']['attributes'][$attribute_id] = $value;
		
		$subproducts = $this->subproduct_ids;
		foreach($this->attributes as $attribute_id => $value) {
			$subproducts = array_merge(array_intersect($subproducts, $this->db->fetchCol("SELECT * FROM shop_products_attributes_values WHERE product_id IN ({$this->subproduct_ids_sql}) AND attribute_id=? AND value=?", array($attribute_id, $value))));
		}
		$this->subproduct = $this->db->fetchRow("SELECT * FROM shop_products WHERE id=? AND deleted=0", $subproducts[0]);
		$this->cache = array();
	}
	
	/**
	 * Set a simple attribute to a value
	 *
	 * @return void
	 * @author Colin Burn-Murdoch
	 **/
	public function setSimpleAttribute($simpleattribute_id, $value) {
		$this->simpleAttributes[$simpleattribute_id] = $value;
		if ($value) $_SESSION['shop']['simpleattributes'][$simpleattribute_id] = $value;
		$this->cache = array();
	}
	
	/**
	 * Generate a hash value representing the currently selected simpleAttributes
	 * This can be used to identify when a product being added to the basket
	 * is already present in the basket - so that the quantity is adjusted 
	 * rather than a new line created.
	 *
	 * @return string
	 **/
	public function simpleAttributesHash() {
		$simpleAttributes = $this->simpleAttributes;
		ksort($simpleAttributes);
		return sha1(serialize($simpleAttributes));
		
	}
	
	/**
	 * Based on currently set attributes, find the values that can be chosen.
	 * This means only values that are available with all other currently selected
	 * values (excluding the one we're currently looking at).
	 * 
	 * @return array
	 **/
	public function attributes() {
		if (isset($this->cache['attributes'])) return $this->cache['attributes'];
		$attributes = array();
		foreach($this->attributes as $attribute_id1 => $value1) {
			$subproducts = $this->subproduct_ids;
			foreach($this->attributes as $attribute_id2 => $value2) {
				if ($attribute_id1 != $attribute_id2) {
					$subproducts = array_intersect($subproducts, $this->db->fetchCol("SELECT product_id FROM shop_products_attributes_values WHERE product_id IN ({$this->subproduct_ids_sql}) AND attribute_id=? AND value=?", array($attribute_id2, $value2)));
				}
			}
			$subproducts_sql = join(',', $subproducts);
			$attributes[$attribute_id1] = $this->db->fetchRow("SELECT * FROM shop_products_attributes WHERE id=?", $attribute_id1);
			$attributes[$attribute_id1]['values'] = $this->db->fetchAll("SELECT av.product_id, av.value, IF(av.value=?, true, false) AS selected FROM shop_products_attributes_values av JOIN shop_products_attributes_products ap ON ap.product_id=? AND ap.attribute_id=av.attribute_id WHERE av.product_id IN ({$subproducts_sql}) AND av.attribute_id=? ORDER BY ap.sort", array($value1, $this->coreproduct['id'], $attribute_id1));
			$attributes[$attribute_id1]['images'] = shop_get_images('attributes', strtolower(preg_replace('/[^a-zA-Z0-9]/u', '-', $attributes[$attribute_id1]['title'])));
			foreach($attributes[$attribute_id1]['values'] as $i => $value) {
				$attributes[$attribute_id1]['values'][$i]['images'] = shop_get_images('attribute-values', strtolower(preg_replace('/[^a-zA-Z0-9]/u', '-', $value['value'])));
				if ($value['selected']) {
					$attributes[$attribute_id1]['value'] = $value['value'];
					$attributes[$attribute_id1]['value_image'] = $attributes[$attribute_id1]['values'][$i]['images'];
				}
				
				$product = new Product($value['product_id']);
				$product->setQuantity($this->quantity);
				
				foreach($this->simpleAttributes as $simpleAttributeId => $simpleAttributeValue) { $product->setSimpleAttribute($simpleAttributeId, $simpleAttributeValue); }
				$attributes[$attribute_id1]['values'][$i]['price']                  = $product->getPrice();
				$attributes[$attribute_id1]['values'][$i]['price_difference']       = $attributes[$attribute_id1]['values'][$i]['price'] - $this->getPrice();
				
				$attributes[$attribute_id1]['values'][$i]['total_price']            = $product->getTotalPrice();
				$attributes[$attribute_id1]['values'][$i]['total_price_difference'] = $attributes[$attribute_id1]['values'][$i]['total_price'] - $this->getTotalPrice();
				
			}
		}
		$this->cache['attributes'] = $attributes;
		return $attributes;
	}
	
	/**
	 * Get the simpleattributes associated with this product/subproduct.
	 *
	 * @return array
	 **/
	public function simpleAttributes() {
		if (isset($this->cache['simpleAttributes'])) return $this->cache['simpleAttributes'];
		$simpleAttributes = $this->db->fetchAll("SELECT * FROM shop_products_simpleattributes_products psp JOIN shop_products_simpleattributes s ON s.id=psp.simpleattribute_id WHERE psp.product_id=? OR psp.product_id=? ORDER BY psp.sort", array($this->coreproduct['id'], $this->subproduct['id']));
		#print_r($this->simpleAttributes);
		#exit;
		
		$simpleAttributesValues = array();
		$simpleAttributesValuesRaw = $this->db->fetchAll("SELECT simpleattribute_id, value, price FROM shop_products_simpleattributes_values WHERE (product_id=? OR product_id=?) ORDER BY sort", array($this->coreproduct['id'], $this->subproduct['id']));
		foreach($simpleAttributesValuesRaw as &$simpleAttributesValue) {
			if (!isset($simpleAttributesValues[$simpleAttributesValue['simpleattribute_id']])) $simpleAttributesValues[$simpleAttributesValue['simpleattribute_id']] = array();
			$simpleAttributesValues[$simpleAttributesValue['simpleattribute_id']][] = $simpleAttributesValue;
		}
		
		
		foreach($simpleAttributes as $i => $simpleAttribute) {
			if ($simpleAttributes[$i]['type'] == 'select') {
				$simpleAttributes[$i]['value']  = null;
				$simpleAttributes[$i]['price']  = 0;
				
				if (!isset($simpleAttributesValues[$simpleAttribute['id']])) {
					// This is a select type but it has no values.
					return;
				}
				
				$simpleAttributes[$i]['values'] = $simpleAttributesValues[$simpleAttribute['id']];
				if (isset($this->simpleAttributes[$simpleAttribute['id']])) {
					foreach($simpleAttributes[$i]['values'] as &$simpleAttributeValue) {
						if ($simpleAttributeValue['value'] == $this->simpleAttributes[$simpleAttribute['id']]) {
							$simpleAttributeValue['selected'] = 1;
							break;
						}
					}
				}

				
				if (!$simpleAttributes[$i]['image_prefix']) $simpleAttributes[$i]['image_prefix'] = strtolower(preg_replace('/[^a-zA-Z0-9]/u', '-', $simpleAttribute['title']));
				$simpleAttributes[$i]['images'] = shop_get_images('attributes', $simpleAttributes[$i]['image_prefix']);
				foreach($simpleAttributes[$i]['values'] as $j => $value) {
					$simpleAttributes[$i]['values'][$j]['images'] = shop_get_images('attribute-values', $simpleAttributes[$i]['image_prefix'] . '_' . strtolower(preg_replace('/[^a-zA-Z0-9]/u', '-', $value['value'])));
					$simpleAttributes[$i]['values'][$j]['price_difference'] = 0;
					$simpleAttributes[$i]['values'][$j]['total_price']      = $value['price'] * $this->quantity;
					if (isset($value['selected']) and $value['selected']) {
						$simpleAttributes[$i]['value'] = $value['value'];
						$simpleAttributes[$i]['price'] = $value['price'];
					}
				}
				foreach($simpleAttributes[$i]['values'] as $j => $value) {
					$simpleAttributes[$i]['values'][$j]['price_difference']       = $value['price'] - $simpleAttributes[$i]['price'];
					$simpleAttributes[$i]['values'][$j]['total_price_difference'] = $simpleAttributes[$i]['values'][$j]['price_difference'] * $this->quantity;
				}
				if ($simpleAttributes[$i]['value'] == null and $simpleAttributes[$i]['values']) {
					$simpleAttributes[$i]['value'] = $simpleAttributes[$i]['values'][0]['value'];
					$simpleAttributes[$i]['price'] = $simpleAttributes[$i]['values'][0]['price'];
				}
			} else {
				if (isset($this->simpleAttributes[$simpleAttributes[$i]['id']])) {
					$simpleAttributes[$i]['value'] = $this->simpleAttributes[$simpleAttributes[$i]['id']];
				} else {
					$simpleAttributes[$i]['value'] = '';
				}
				$simpleAttributes[$i]['price'] = 0;
			}
		}
		$this->cache['simpleAttributes'] = $simpleAttributes;
		return $simpleAttributes;
	}
	
	public function meta() {
		if (isset($this->cache['meta'])) return $this->cache['meta'];
		$meta = $this->db->fetchPairs("SELECT  pm.title, pmv.value FROM shop_products_metas_values pmv LEFT JOIN shop_products_metas pm ON pmv.meta_id=pm.id WHERE pmv.product_id=? OR pmv.product_id=? ORDER BY pmv.sort", array($this->coreproduct['id'], $this->subproduct['id']));
		$this->cache['meta'] = $meta;
		return $meta;
	}
	
	public function categories() {
		$referer = isset($_SERVER['HTTP_REFERER']) ? array_filter(explode('/', preg_replace('/^https?:\/\/.*?\/(.*?)/', "$1", $_SERVER['HTTP_REFERER']))) : false;
		
		$raw_categories = $this->db->fetchAll("SELECT c.id, c.parent_id, c.title, c.path FROM shop_categories_products cp LEFT JOIN shop_categories c ON cp.category_id=c.id WHERE cp.product_id=? AND cp.master=1", $this->coreproduct['id']);
		$categories = array('referer' => false, 'all' => array());
		foreach($raw_categories as $raw_category) {
			$category = array(
				array(
					'title' => $raw_category['title'],
					'path'  => $raw_category['path']
				)
			);
			$parent_id = $raw_category['parent_id'];
			while($parent_id) {
				$raw_category = $this->db->fetchRow("SELECT id, parent_id, title, path FROM shop_categories WHERE id=?", $parent_id);
				$category[] = array(
					'title' => $raw_category['title'],
					'path'  => $raw_category['path']
				);
				$parent_id = $raw_category['parent_id'];
			}
			$category = array_reverse($category);
			
			$full_path = array();
			foreach($category as $i => $chunk) {
				$full_path[] = $chunk['path'];
				$category[$i]['full_path'] = implode($full_path, '/') . '/';
			}
			
			$categories['all'][] = $category;
			
			if (!$categories['referer'] and $referer) {
				foreach($category as $i => $chunk) {
					if ($chunk['path'] != $referer[$i]) break;
					if ($i == count($referer)-1) {
						$categories['referer'] = array_slice($category, 0, $i);
						break;
					}
				}
			}
		}
		
		return $categories;
	}
	
	private function valueInclVat($value, $rate) {
		global $env;
		if (isset($env['shop']['vat']) and $env['shop']['vat'] == 'excl') {
			return number_format($value*(1+($rate/100)), 2, '.', '');
		} else {
			return $value;
		}
	}
	
	private function valueExclVat($value, $rate) {
		global $env;
		if (isset($env['shop']['vat']) and $env['shop']['vat'] == 'excl') {
			return $value;
		} else {
			return number_format($value/(1+($rate/100)), 2, '.', '');
		}
	}
	
	public function range() {
		$products = $this->db->fetchCol("SELECT id FROM shop_products WHERE range_id=? AND id!=? AND active=1 AND deleted=0", array($this->coreproduct['range_id'], $this->coreproduct['id']));
		foreach($products as $i => $product_id) {
			$products[$i] = new Product($product_id);
			$products[$i] = $products[$i]->export();
		}
		return $products;
	}
	
	public function related() {
		$products = $this->db->fetchCol("SELECT related_id FROM shop_products_related r JOIN shop_products p ON r.related_id=p.id WHERE r.product_id=? AND p.active=1 AND p.deleted=0", $this->coreproduct['id']);
		foreach($products as $i => $product_id) {
			$products[$i] = new Product($product_id);
			$products[$i] = $products[$i]->export();
		}
		return $products;
	}
	
}
